package cn.com.bsoft.cloud.controller.company;

import cn.com.bsoft.cloud.entity.cloud.*;
import cn.com.bsoft.cloud.entity.company.CompanyMess;
import cn.com.bsoft.cloud.service.CloudCompanyService;
import cn.com.bsoft.cloud.service.CloudMessService;
import cn.com.bsoft.cloud.utils.ResultCode;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/cloudmess/")
public class CloudMessController {

    public static Logger logger= LogManager.getLogger(CloudMessController.class);

    @Autowired
    private CloudMessService cloudMessService;

    @Autowired
    private CloudCompanyService cloudCompanyService;


    /**
     * 查询用药途径
     * @return
     */
    @ResponseBody
    @RequestMapping("queryRoute")
    public Map<String,Object> queryRoute(@RequestBody Map<String,String> map){
        Map<String,Object> regMsg=new HashMap<String, Object>();
        String routeCode=map.get("routeCode");
        try {
            List<RouteMess> list=new ArrayList<RouteMess>();
            if (StringUtils.isBlank(routeCode)) {
                //查询所有
               list= cloudMessService.queryAllRoute();
            }else{
                //根据用药途径编码查用药名称
                list= cloudMessService.findRouteByCode(routeCode);
            }
                regMsg.put("code", ResultCode.SUCCESS);
                regMsg.put("msg", "查询成功");
                regMsg.put("result", list);
        } catch (Exception e) {
            regMsg.put("code", ResultCode.ERROR);
            regMsg.put("msg", "请求出错");
            regMsg.put("error", e.getMessage());
            logger.error("查询用药途径,参数:"+map+"，异常"+e.getMessage(),e);
        }
        return regMsg;
    }


    /**
     * 查询用药频次
     * @return
     */
    @ResponseBody
    @RequestMapping("queryFrequency")
    public Map<String,Object> queryFrequency(@RequestBody Map<String,String> map){
        Map<String,Object> regMsg=new HashMap<String, Object>();
        String frequencyCode=map.get("frequencyCode");
        try {
            List<FrequencyMess> list=new ArrayList<FrequencyMess>();
            if (StringUtils.isBlank(frequencyCode)) {
                //查询所有
                list= cloudMessService.queryAllFrequency();
            }else{
                //根据用药途径编码查用药名称
                list= cloudMessService.findFrequencyByCode(frequencyCode);
            }
            regMsg.put("code", ResultCode.SUCCESS);
            regMsg.put("msg", "查询成功");
            regMsg.put("result", list);
        } catch (Exception e) {
            regMsg.put("code", ResultCode.ERROR);
            regMsg.put("msg", "请求出错");
            regMsg.put("error", e.getMessage());
            logger.error("查询用药频次,参数:"+map+"，异常"+e.getMessage(),e);
        }
        return regMsg;
    }


    /**
     * 查询剂型信息
     * @return
     */
    @ResponseBody
    @RequestMapping("queryFormulation")
    public Map<String,Object> queryFormulation(@RequestBody Map<String,String> map){
        Map<String,Object> regMsg=new HashMap<String, Object>();
        String formulationCode=map.get("formulationCode");
        try {
            List<FormulationMess> list=new ArrayList<FormulationMess>();
            if (StringUtils.isBlank(formulationCode)) {
                //查询所有
                list= cloudMessService.queryAllFormulation();
            }else{
                //根据用药途径编码查用药名称
                list= cloudMessService.findFormulationByCode(formulationCode);
            }
            regMsg.put("code", ResultCode.SUCCESS);
            regMsg.put("msg", "查询成功");
            regMsg.put("result", list);
        } catch (Exception e) {
            regMsg.put("code", ResultCode.ERROR);
            regMsg.put("msg", "请求出错");
            regMsg.put("error", e.getMessage());
            logger.error("查询剂型信息,参数:"+map+"，异常"+e.getMessage(),e);
        }
        return regMsg;
    }


    /**
     * 查询分站点信息
     * @return
     */
    @ResponseBody
    @RequestMapping("querySite")
    public Map<String,Object> querySite(@RequestBody Map<String,String> map){
        Map<String,Object> regMsg=new HashMap<String, Object>();
        String hosCode=map.get("hosCode");
        String siteCode=map.get("siteCode");
        try {
            List<SiteMess> list=new ArrayList<SiteMess>();
            if (StringUtils.isBlank(hosCode)&&StringUtils.isBlank(siteCode)) {
                //查询所有
                list= cloudMessService.querySite();
            }else if(StringUtils.isNotBlank(hosCode)&&StringUtils.isBlank(siteCode)){
                //根据医院下的所有分站点
                list= cloudMessService.querySiteByHoscode(hosCode);
            }else{
                list= cloudMessService.querySiteByCode(hosCode,siteCode);
            }

            regMsg.put("code", ResultCode.SUCCESS);
            regMsg.put("msg", "查询成功");
            regMsg.put("result", list);
        } catch (Exception e) {
            regMsg.put("code", ResultCode.ERROR);
            regMsg.put("msg", "请求出错");
            regMsg.put("error", e.getMessage());
            logger.error("查询分站点信息，异常"+e.getMessage(),e);
        }
        return regMsg;
    }


    /**
     * 查询剂量单位
     * @return
     */
    @ResponseBody
    @RequestMapping("queryDose")
    public Map<String,Object> queryDose(@RequestBody Map<String,String> map){
        Map<String,Object> regMsg=new HashMap<String, Object>();
        String doseCode=map.get("doseCode");
        try {
            List<DoseMess> list=new ArrayList<DoseMess>();
            if (StringUtils.isBlank(doseCode)) {
                //查询所有
                list= cloudMessService.queryAllDoses();
            }else{
                //根据用药途径编码查用药名称
                list= cloudMessService.findDoseByCode(doseCode);
            }
            regMsg.put("code", ResultCode.SUCCESS);
            regMsg.put("msg", "查询成功");
            regMsg.put("result", list);
        } catch (Exception e) {
            regMsg.put("code", ResultCode.ERROR);
            regMsg.put("msg", "请求出错");
            regMsg.put("error", e.getMessage());
            logger.error("查询计量单位信息,参数:"+map+"，异常"+e.getMessage(),e);
        }
        return regMsg;
    }


    /**
     * 查询医院信息
     * @return
     */
    @ResponseBody
    @RequestMapping("queryHospitalMess")
    public Map<String,Object> queryHospitalMess(){
        Map<String,Object> regMsg=new HashMap<String, Object>();
        try {
            List<HospitalResult> list=cloudMessService.queryHospitalMess();
            regMsg.put("code", ResultCode.SUCCESS);
            regMsg.put("msg", "查询成功");
            regMsg.put("result", list);
        } catch (Exception e) {
            regMsg.put("code", ResultCode.ERROR);
            regMsg.put("msg", "请求出错");
            regMsg.put("error", e.getMessage());
            logger.error("查询所有医院异常"+e.getMessage(),e);
        }
        return regMsg;
    }


    /**
     * 查询医药公司相关信息
     * @return
     */
    @ResponseBody
    @RequestMapping("queryCompanyMess")
    public Map<String,Object> queryCompanyMess(@RequestBody Map<String,String> map){

        Map<String,Object> regMsg=new HashMap<String, Object>();
        String companyCode=map.get("companyCode");
        try {
            List<CompanyMess> list= cloudCompanyService.queryCompanyMess(companyCode);
            regMsg.put("code", ResultCode.SUCCESS);
            regMsg.put("msg", "查询成功");
            regMsg.put("result", list);
        } catch (Exception e) {
            regMsg.put("code", ResultCode.ERROR);
            regMsg.put("msg", "请求出错");
            regMsg.put("error", e.getMessage());
            logger.error("查询所有医药公司相关信息，异常"+e.getMessage(),e);
        }
        return regMsg;
    }


    /**
     * 更改医药公司公钥私钥
     * @return
     */
    @ResponseBody
    @RequestMapping("updateCompanyMess")
    public Map<String,Object> updateCompanyMess(@RequestBody Map<String,String> map){

        Map<String,Object> regMsg=new HashMap<String, Object>();
        try {
            String secret=map.get("secret");
            String publickey=map.get("publickey");
            String companyCode=map.get("companyCode");
            String companyName=map.get("companyName");
            if(StringUtils.isBlank(secret)){
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "秘钥不能为空");
                return regMsg;
            }
            if(StringUtils.isBlank(companyCode)){
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "医药公司编码不能为空");
                return regMsg;
            }
            if(StringUtils.isBlank(companyName)){
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "医药公司名称不能为空");
                return regMsg;
            }
            if(StringUtils.isBlank(publickey)){
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "公钥不能为空");
                return regMsg;
            }
            Boolean b=cloudCompanyService.updateCompanyMess(companyCode,companyName,secret,publickey);
            regMsg.put("code", ResultCode.SUCCESS);
            regMsg.put("msg", "更改成功");
        } catch (Exception e) {
            regMsg.put("code", ResultCode.ERROR);
            regMsg.put("msg", "请求出错");
            regMsg.put("error", e.getMessage());
            logger.error("查询医药公司相关信息，异常"+e.getMessage(),e);
        }
        return regMsg;
    }



    /**
     * 添加医药公司相关信息
     * @return
     */
    @ResponseBody
    @RequestMapping("addCompanyMess")
    public Map<String,Object> addCompanyMess(@RequestBody Map<String,String> map){

        Map<String,Object> regMsg=new HashMap<String, Object>();
        try {
            String secret=map.get("secret");
            String publickey=map.get("publickey");
            String companyCode=map.get("companyCode");
            String companyName=map.get("companyName");
            if(StringUtils.isBlank(secret)){
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "秘钥不能为空");
                return regMsg;
            }
            if(StringUtils.isBlank(companyCode)){
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "医药公司编码不能为空");
                return regMsg;
            }
            if(StringUtils.isBlank(companyName)){
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "医药公司名称不能为空");
                return regMsg;
            }
            if(StringUtils.isBlank(publickey)){
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "公钥不能为空");
                return regMsg;
            }
            //判断是否已经存在此医药公司
            String name=cloudCompanyService.findCompanyNameByCode(companyCode);
            if(StringUtils.isNotBlank(name)){
                regMsg.put("code", ResultCode.ERROR);
                regMsg.put("msg", "已存在次医药公司，不能继续添加");
            }else{
                Boolean b=cloudCompanyService.addCompanyMess(companyCode,companyName,secret,publickey);
                regMsg.put("code", ResultCode.SUCCESS);
                regMsg.put("msg", "添加成功");
            }
        } catch (Exception e) {
            regMsg.put("code", ResultCode.ERROR);
            regMsg.put("msg", "请求出错");
            regMsg.put("error", e.getMessage());
            logger.error("添加医药公司相关信息，异常"+e.getMessage(),e);
        }
        return regMsg;
    }


    /**
     * 获取公钥
     * @return
     */
    @ResponseBody
    @RequestMapping("findCompanyPubkey")
    public Map<String,Object> findCompanyPubkey(){

        Map<String,Object> regMsg=new HashMap<String, Object>();
        try {
            String pubkey=cloudCompanyService.findCompanyPubkey();
            regMsg.put("code", ResultCode.SUCCESS);
            regMsg.put("msg", "查询成功");
            regMsg.put("result", pubkey);
        } catch (Exception e) {
            regMsg.put("code", ResultCode.ERROR);
            regMsg.put("msg", "请求出错");
            regMsg.put("error", e.getMessage());
            logger.error("查询医药公司相关信息，异常"+e.getMessage(),e);
        }
        return regMsg;
    }


    /**
     * 查询所有分站点大类列表接口
     * @return
     */
    @ResponseBody
    @RequestMapping("queryHosList")
    public Map<String,Object> queryHosList(){
        Map<String,Object> regMsg=new HashMap<String, Object>();
        try {
            List<Site> list=cloudCompanyService.queryHosList();
            regMsg.put("code", ResultCode.SUCCESS);
            regMsg.put("msg", "查询所有分站点大类列表成功");
            regMsg.put("result", list);
        } catch (Exception e) {
            regMsg.put("code", ResultCode.ERROR);
            regMsg.put("msg", "请求出错");
            regMsg.put("error", e.getMessage());
            logger.error("查询所有分站点大类列表，异常"+e.getMessage(),e);
        }
        return regMsg;
    }


    /**
     * 查询具体社区分站点信息
     * @return
     */
    @ResponseBody
    @RequestMapping("querySiteByCode")
    public Map<String,Object> querySiteByCode(@RequestBody Map<String,String> map){

        Map<String,Object> regMsg=new HashMap<String, Object>();

        try {
            String hosCode=map.get("hosCode");
            if(StringUtils.isBlank(hosCode)){
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "社区编码不能为空");
                return regMsg;
            }
            //查询具体社区分站点信息
            List<Site> sitelist=cloudCompanyService.querySiteByCode(hosCode);
            regMsg.put("code", ResultCode.SUCCESS);
            regMsg.put("msg", "查询具体社区分站点信息");
            regMsg.put("result", sitelist);
        } catch (Exception e) {
            regMsg.put("code", ResultCode.ERROR);
            regMsg.put("msg", "请求出错");
            regMsg.put("error", e.getMessage());
            logger.error("查询具体社区分站点信息，异常"+e.getMessage(),e);
        }
        return regMsg;
    }


    /**
     * 更改配送点地址
     * @return
     */
    @ResponseBody
    @RequestMapping("updateAddressByCode")
    public Map<String,Object> updateAddressByCode(@RequestBody Map<String,String> map){

        Map<String,Object> regMsg=new HashMap<String, Object>();
        try {
            String siteCode=map.get("siteCode");
            String siteName=map.get("siteName");
            String address=map.get("address");
            if(StringUtils.isBlank(siteCode)){
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "分站点编码不能为空");
                return regMsg;
            }
            if(StringUtils.isBlank(siteName)){
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "分站点名称不能为空");
                return regMsg;
            }
            if(StringUtils.isBlank(address)){
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "分站点地址不能为空");
                return regMsg;
            }
            String name=cloudCompanyService.findSiteBySitecode(siteCode);
            if (StringUtils.isBlank(name)) {
                regMsg.put("code", ResultCode.ERROR);
                regMsg.put("msg", "不存在此站点信息，请检查后重新操作");
                return regMsg;
            }
            cloudCompanyService.updateAddressByCode(siteCode,siteName,address);
            regMsg.put("code", ResultCode.SUCCESS);
            regMsg.put("msg", "更改配送点地址成功");
        } catch (Exception e) {
            regMsg.put("code", ResultCode.ERROR);
            regMsg.put("msg", "请求出错");
            regMsg.put("error", e.getMessage());
            logger.error("更改配送点地址，异常"+e.getMessage(),e);
        }
        return regMsg;
    }



    /**
     * 添加配送分站点信息
     * @return
     */
    @ResponseBody
    @RequestMapping("addSiteMess")
    public Map<String,Object> addSiteMess(@RequestBody Map<String,String> map){

        Map<String,Object> regMsg=new HashMap<String, Object>();
        try {
            String hosCode=map.get("hosCode");
            String hosName=map.get("hosName");
            String siteCode=map.get("siteCode");
            String siteName=map.get("siteName");
            String address=map.get("address");
            if(StringUtils.isBlank(hosCode)){
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "社区编码不能为空");
                return regMsg;
            }
            if(StringUtils.isBlank(hosName)){
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "社区名称不能为空");
                return regMsg;
            }
            if(StringUtils.isBlank(siteCode)){
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "分站点编码不能为空");
                return regMsg;
            }
            if(StringUtils.isBlank(siteName)){
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "分站点名称不能为空");
                return regMsg;
            }
            if(StringUtils.isBlank(address)){
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "分站点地址不能为空");
                return regMsg;
            }
            String name=cloudCompanyService.findSiteBySitecode(siteCode);
            if (StringUtils.isNotBlank(name)) {
                regMsg.put("code", ResultCode.ERROR);
                regMsg.put("msg", "此分站点编码已存在");
                return regMsg;
            }
            cloudCompanyService.addSiteMess(hosCode,hosName,siteCode,siteName,address);
            regMsg.put("code", ResultCode.SUCCESS);
            regMsg.put("msg", "添加配送分站点信息成功");
        } catch (Exception e) {
            regMsg.put("code", ResultCode.ERROR);
            regMsg.put("msg", "请求出错");
            regMsg.put("error", e.getMessage());
            logger.error("添加配送分站点信息，异常"+e.getMessage(),e);
        }
        return regMsg;
    }

    /**
     * 作废分站点信息
     * @return
     **/
    @ResponseBody
    @RequestMapping("delSiteMessByCode")
    public Map<String,Object> delSiteMessByCode(@RequestBody Map<String,String> map){

        Map<String,Object> regMsg=new HashMap<String, Object>();
        try {
            String siteCode=map.get("siteCode");

            if(StringUtils.isBlank(siteCode)){
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "分站点编码不能为空");
                return regMsg;
            }
            cloudCompanyService.delSiteMessByCode(siteCode);
            regMsg.put("code", ResultCode.SUCCESS);
            regMsg.put("msg", "作废分站点信息成功");
        } catch (Exception e) {
            regMsg.put("code", ResultCode.ERROR);
            regMsg.put("msg", "请求出错");
            regMsg.put("error", e.getMessage());
            logger.error("作废分站点信息，异常"+e.getMessage(),e);
        }
        return regMsg;
    }



}
